package com.leyou.cart.service.impl;

import com.leyou.auth.pojo.UserInfo;
import com.leyou.auth.utils.JwtUtils;
import com.leyou.cart.config.JwtProperties;
import com.leyou.cart.interceptors.UserInterceptor;
import com.leyou.cart.pojo.Cart;
import com.leyou.cart.service.CartService;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exception.LyException;
import com.leyou.common.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@EnableConfigurationProperties(JwtProperties.class)
public class CartServiceImpl implements CartService {

    @Autowired
    private JwtProperties jwtProp;


    @Autowired
    private StringRedisTemplate redisTemplate;

    private static final String KEY_PREFIX = "cart:uid:";

    @Override
    public void addCart(Cart cart) {

        // 获取key,即用户id
        UserInfo userInfo = UserInterceptor.getUser();
        String key = KEY_PREFIX + userInfo.getId();
        // 获取hashKey
        String hashKey = cart.getSkuId().toString();

        BoundHashOperations<String, String, String> operation = redisTemplate.boundHashOps(key);
        // 判断hashKey是否存在
        if (operation.hasKey(hashKey)) {
            // 获取传来的cart的num
            Integer num = cart.getNum();
            // 存在,加操作
            cart = JsonUtils.toBean(operation.get(hashKey), Cart.class);

            cart.setNum(cart.getNum() + num);
        }

        // 不存在,直接添加
        operation.put(hashKey, JsonUtils.toString(cart));
    }

    @Override
    public List<Cart> queryCartList() {

        // 获取key,即用户id
        UserInfo userInfo = UserInterceptor.getUser();
        String key = KEY_PREFIX + userInfo.getId();

        // 判断购物车是否存在
        if (!redisTemplate.hasKey(key)) {
            throw new LyException(ExceptionEnum.CART_NOT_FOUND);
        }
        BoundHashOperations<String, String, String> operation = redisTemplate.boundHashOps(key);

        return operation.values().stream().map(s -> JsonUtils.toBean(s, Cart.class)).collect(Collectors.toList());
    }

    @Override
    public void updateCartNum(Long skuId, Integer num) {

        // 获取key,即用户id
        UserInfo userInfo = UserInterceptor.getUser();
        String key = KEY_PREFIX + userInfo.getId();

        String stringSkuId = skuId.toString();
        BoundHashOperations<String, String, String> operation = redisTemplate.boundHashOps(key);
        // 获取购物车商品
        Cart cacheCart = JsonUtils.toBean(operation.get(stringSkuId), Cart.class);
        cacheCart.setNum(num);
        operation.put(stringSkuId, JsonUtils.toString(cacheCart));
    }

    @Override
    public void deleteCart(Long skuId) {

        // 获取key,即用户id
        UserInfo userInfo = UserInterceptor.getUser();
        String key = KEY_PREFIX + userInfo.getId();

        redisTemplate.opsForHash().delete(key, skuId.toString());
    }
}
